package com.dna.utils.shiro;

import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dna.entity.UserEntity;
import com.dna.service.MenuService;
import com.dna.service.UserService;

/**
 * 认证
 * 
 */
@Component
public class UserRealm extends AuthorizingRealm {
	
	@Autowired
	private UserService userService;

	@Autowired
	private MenuService menuService;
	
	/**
	 * 授权(验证权限时调用)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		UserEntity userEntity = (UserEntity)principals.getPrimaryPrincipal();

		//用户权限列表
		Set<String> permsSet = menuService.queryPerByRoleId(userEntity.getRoleId());

		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.setStringPermissions(permsSet);
		return info;
	}

	/**
	 * 认证(登录时调用)
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		
		String username = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        
        userEntity = userService.queryOne(userEntity);
        
        //账号不存在
        if(userEntity == null) {
            throw new UnknownAccountException("账号或密码不正确");
        }
        //密码错误
        if(!password.equals(userEntity.getPassword())) {
            throw new IncorrectCredentialsException("账号或密码不正确");
        }
        //账号锁定
        if(userEntity.getState() == 0){
        	throw new LockedAccountException("账号已被锁定,请联系管理员");
        }
        
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userEntity, password, getName());
        return info;
	}
}
